package com.example.administrator.runbook.fragments.community.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.runbook.R;
import com.example.administrator.runbook.fragments.community.model.Comment;
import com.example.administrator.runbook.fragments.community.model.Tweets;
import com.example.administrator.runbook.fragments.community.utils.CircleTransform;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Administrator on 2016/11/8.
 */

public class TrendsDetailListAdapter extends BaseAdapter {

    private List<Comment> list;
    private Tweets tweets;
    private Context context;
    private LayoutInflater inflater;

    public TrendsDetailListAdapter(Context context, List<Comment> list, Tweets tweets) {
        this.context = context;
        this.list = list;
        this.tweets = tweets;
        inflater = LayoutInflater.from(context);
    }

    public void setTweets(Tweets tweets) {
        this.tweets = tweets;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return 0;
        } else if (position == 1) {
            return 1;
        } else
            return 2;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            if (getItemViewType(position) == 0) {
                convertView = inflater.inflate(R.layout.item_detail_activity_detail, parent, false);
                convertView.setTag(new ViewHoldDetail(convertView));
            }
            if (getItemViewType(position) == 1) {
                convertView = inflater.inflate(R.layout.item_text_activity_detail, parent, false);
                convertView.setTag(new ViewHoldText(convertView));
            }
            if (getItemViewType(position) == 2) {
                convertView =
                        inflater.inflate(R.layout.item_comment_activity_detail, parent, false);
                convertView.setTag(new ViewHoldComment(convertView));
            }
        }
        if (getItemViewType(position) == 0) {
            ViewHoldDetail hold = (ViewHoldDetail) convertView.getTag();
            hold.bindView(position, tweets);
        } else if (getItemViewType(position) == 1) {
            ViewHoldText hold = (ViewHoldText) convertView.getTag();
            hold.bindView(position, tweets);
        } else if (getItemViewType(position) == 2) {
            ViewHoldComment hold = (ViewHoldComment) convertView.getTag();
            Comment comment = list.get(position);
            hold.bindView(position, comment);
        }
        return convertView;
    }

    private static class ViewHoldDetail {

        private ImageView imageView;
        private TextView textName;
        private TextView textLv;
        private TextView textDate;
        private TextView textTitle;
        private TextView textContext;

        public ViewHoldDetail(View itemView) {
            imageView = (ImageView) itemView.findViewById(R.id.image_detail_activity);
            textName = (TextView) itemView.findViewById(R.id.name_detail_activity);
            textLv = (TextView) itemView.findViewById(R.id.lv_detail_activity);
            textDate = (TextView) itemView.findViewById(R.id.date_detail_activity);
            textTitle = (TextView) itemView.findViewById(R.id.title_detail_activity);
            textContext = (TextView) itemView.findViewById(R.id.context_detail_activity);
        }

        public void bindView(int position, Tweets data) {

            Picasso.with(imageView.getContext())
                    .load(data.getAvatar())
                    .transform(new CircleTransform())
                    .placeholder(R.mipmap.avatar_default)
                    .resizeDimen(R.dimen.avatar_size, R.dimen.avatar_size)
                    .error(R.mipmap.avatar_default)
                    .into(imageView);
            textName.setText(data.getNickname());
            textLv.setText("lv." + data.getLv());
            textDate.setText(data.getData(data.getCreated()));
            textTitle.setText(data.getTitle());
            textContext.setText(data.getContent());
        }
    }

    private static class ViewHoldText {

        private TextView textComment;

        public ViewHoldText(View itemView) {
            textComment = (TextView) itemView.findViewById(R.id.comment_text_activity_detail);
        }

        public void bindView(int position, Tweets data) {

            textComment.setText(data.getCommented() + "条评论");
        }
    }

    private static class ViewHoldComment {

        private ImageView imageView;
        private TextView textName;
        private TextView textLv;
        private TextView textDate;
        private TextView textCommit;
        private TextView textContext;
        private TextView textFloor;

        public ViewHoldComment(View itemView) {
            imageView = (ImageView) itemView.findViewById(R.id.image_comment_detail_activity);
            textName = (TextView) itemView.findViewById(R.id.name_comment_detail_activity);
            textLv = (TextView) itemView.findViewById(R.id.lv_comment_detail_activity);
            textDate = (TextView) itemView.findViewById(R.id.date_comment_detail_activity);
            textCommit = (TextView) itemView.findViewById(R.id.commit_comment_detail_activity);
            textContext = (TextView) itemView.findViewById(R.id.context_comment_detail_activity);
            textFloor = (TextView) itemView.findViewById(R.id.floor_comment_detail_activity) ;
        }

        public void bindView(int position, Comment data) {

            Picasso.with(imageView.getContext())
                    .load(data.getAvatar())
                    .transform(new CircleTransform())
                    .placeholder(R.mipmap.avatar_default)
                    .resizeDimen(R.dimen.avatar_size, R.dimen.avatar_size)
                    .error(R.mipmap.avatar_default)
                    .into(imageView);
            textFloor.setText(data.getFloor() + "楼");
            textName.setText(data.getNickname());
            textLv.setText("lv." + data.getLv());
            textDate.setText(data.getData(data.getCreated()));
            if (data.getReId() == null) {
                textCommit.setVisibility(View.INVISIBLE);
            } else {
                textCommit.setVisibility(View.VISIBLE);
                textCommit.setText("回复 " + data.getRenickname() + "(" + data.getRefloor() + "楼)");
            }
            textContext.setText(data.getContent());
        }
    }

}
